#!/usr/bin/env python3
# -*- coding: utf-8 -*-


import pymysql
import tushare as ts
import time

"""
2.1查询操作
#打开数据库连接
db= pymysql.connect(host="localhost",user="root",
 	password="123456",db="test",port=3307)
 
# 使用cursor()方法获取操作游标
cur = db.cursor()
 
#1.查询操作
# 编写sql 查询语句  user 对应我的表名
sql = "select * from user"
try:
	cur.execute(sql) 	#执行sql语句
 
	results = cur.fetchall()	#获取查询的所有记录
	print("id","name","password")
	#遍历结果
	for row in results :
		id = row[0]
		name = row[1]
		password = row[2]
		print(id,name,password)
except Exception as e:
	raise e
finally:
	db.close()	#关闭连接


2.2插入操作
#2.插入操作
db= pymysql.connect(host="localhost",user="root",
 	password="123456",db="test",port=3307)
 
# 使用cursor()方法获取操作游标
cur = db.cursor()
 
sql_insert ="insert into user(id,username,password) values(4,'liu','1234')"
 
try:
	cur.execute(sql_insert)
	#提交
	db.commit()
except Exception as e:
	#错误回滚
	db.rollback()
finally:
	db.close()


2.3更新操作
#3.更新操作
db= pymysql.connect(host="localhost",user="root",
 	password="123456",db="test",port=3307)
 
# 使用cursor()方法获取操作游标
cur = db.cursor()
 
sql_update ="update user set username = '%s' where id = %d"
 
try:
	cur.execute(sql_update % ("xiongda",3))  #像sql语句传递参数
	#提交
	db.commit()
except Exception as e:
	#错误回滚
	db.rollback() 
finally:
	db.close()
--------------------- 


2.4删除操作
#4.删除操作
db= pymysql.connect(host="localhost",user="root",
 	password="123456",db="test",port=3307)
 
# 使用cursor()方法获取操作游标
cur = db.cursor()
 
sql_delete ="delete from user where id = %d"
 
try:
	cur.execute(sql_delete % (3))  #像sql语句传递参数
	#提交
	db.commit()
except Exception as e:
	#错误回滚
	db.rollback() 
finally:
	db.close()
--------------------- 
"""


ts.set_token('b81d01e83e670dc4c0313032df361210a3b1141df4709c3aabe25b88')
#初始化pro接口

pro = ts.pro_api()

#查询当前所有正常上市交易的股票列表
stockbasic = pro.stock_basic(exchange='', list_status='L', fields='ts_code,symbol,name,area,industry,fullname,market,exchange,curr_type, \
                             list_status,list_date')
print ('get Basic  %s' % time.asctime( time.localtime(time.time()) ) )

# 打开数据库连接
db = pymysql.connect(host='localhost', 
                        user='root', 
                        passwd='taotao007', 
                        db='stock',
                        use_unicode=True,
                        charset='utf8')

cur = db.cursor()

sql_delete ="delete from stock_list"
cur.execute(sql_delete) #像sql语句传递参数
#提交
db.commit()
    

for index,row in stockbasic.iterrows():
    sql_insert ="insert into stock_list(stock_basic_ts_code,stock_basic_symbol,stock_basic_name,stock_basic_area,stock_basic_industry, \
    stock_basic_fullname,stock_basic_market,stock_basic_exchange,stock_basic_curr_type,stock_basic_list_status,stock_basic_list_date) \
    values('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s');" % (row['ts_code'],row['symbol'],row['name'],row['area'], \
    row['industry'],row['fullname'],row['market'],row['exchange'],row['curr_type'],row['list_status'], \
    row['list_date'])
    cur.execute(sql_insert)

try:
	db.commit()
except Exception as e:
	#错误回滚
	db.rollback()
finally:
	db.close()
    
print ('finished stock basic ')

'''
#上市公司基本信息  SSE

stockcompany_SSE = pro.stock_company(exchange='SSE', fields='ts_code,chairman,manager,province,city,main_business,business_scope')
print ('get SSE  %s ' % time.asctime( time.localtime(time.time()) ))
#write to db
db = pymysql.connect(host='localhost', 
                        user='root', 
                        passwd='taotao007', 
                        db='stock',
                        use_unicode=True,
                        charset='utf8')

cur = db.cursor()
sql_delete ="delete from stock_company"
cur.execute(sql_delete) #像sql语句传递参数
db.commit()

for index,row in stockcompany_SSE.iterrows():
    sql_insert ="insert into stock_company(stock_company_ts_code,stock_company_chairman,stock_company_manager,stock_company_province, \
    stock_company_city,stock_company_main_business,stock_company_business_scope) \
    values('%s','%s','%s','%s','%s','%s','%s');" % ( row['ts_code'],row['chairman'],row['manager'],row['province'],row['city'],row['main_business'],row['business_scope'])
    print (sql_insert)
    cur.execute(sql_insert)
    
try:
	#提交
	db.commit()
except Exception as e:
	#错误回滚
	db.rollback()
finally:
	db.close()

print ('finished stock basic SSE ')

#上市公司基本信息  SZSE
stockcompay_SZSE = pro.stock_company(exchange='SZSE', fields='ts_code,chairman,manager,province,city,main_business,business_scope')
print ('get SZSE  %s' % time.asctime( time.localtime(time.time()) ))
#write to db
db = pymysql.connect(host='localhost', 
                        user='root', 
                        passwd='taotao007', 
                        db='stock',
                        use_unicode=True,
                        charset='utf8')

cur = db.cursor()
 
for index,row in stockcompay_SZSE.iterrows():
    sql_insert ="insert into stock_company(stock_company_ts_code,stock_company_chairman,stock_company_manager,stock_company_province, \
    stock_company_city,stock_company_main_business,stock_company_business_scope) \
    values('%s','%s','%s','%s','%s','%s','%s');" & (row['ts_code'],row['chairman'],row['manager'],row['province'],row['city'],row['main_business'],row['business_scope']) 
    cur.execute(sql_insert)
    print (sql_insert)
try:
	#提交
	db.commit()
except Exception as e:
	#错误回滚
	db.rollback()
finally:
	db.close()
    
print ('finished stock basic SZSE ')
'''

